﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using PagedList;

namespace LKVMLMADMIN.Controllers.Html
{
    public class GetHelpController : Controller
    {
        public LKVService.Transaction trans = new LKVService.Transaction();
        public LKVService.User db = new LKVService.User();
        public ActionResult ListGH(int page = 1,string status = "", string Fromdate = "", string Todate = "", string Username = "", string message = "")
        {
            if (Session["User"] == null)
                return RedirectToAction("Login", "Account");
            ViewBag.Msg = message;
            DateTime fromdate;
            DateTime todate;
            int Stt = -1;
            if (Fromdate == "")
                fromdate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            else
            {
                fromdate = Convert.ToDateTime(Fromdate);
                ViewBag.DateFrom = Fromdate;
            }
            if (Todate == "")
                todate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);
            else
            {
                todate = Convert.ToDateTime(Todate);
                ViewBag.Todate = Todate;
            }
            if (status != "")
                Stt = Convert.ToInt32(status);
            var ls = trans.getListGHForAdmin(Username, Stt, fromdate, todate, (page - 1) * 25, 25);
            int totalItems = ls.Item1;
            var lsGH = ls.Item2.ToList();
            ViewBag.status = status;
            ViewBag.GHs = lsGH;
            int[] myArray = Enumerable.Repeat(-1, ls.Item1).ToArray();
            return View(myArray.ToPagedList(page, 25));
        }
        public ActionResult CreateTransferOrder(int userGH = 0, int ghId = 0)
        {
            if (userGH == 0 || ghId == 0)
                return RedirectToAction("ListGH", "GetHelp", new { message = "Không thành công !!!" });
            var kq = trans.pairingPHAndGH(userGH, ghId);
            if (kq.Equals("***"))
            {
                return RedirectToAction("ListGH", "GetHelp", new { message = "Thành công !!!" });
            }
            else
                return RedirectToAction("ListGH", "GetHelp", new { message = kq });
        }
        public ActionResult Prioritize(string message = "")
        {
            if (Session["User"] == null)
                return RedirectToAction("Login", "Account");
            ViewBag.Msg = message;
            //lấy danh sách GH ưu tiên group 7
            var lsAdmin = db.getListUserByGroupNotActive(7).ToList();
            ViewBag.UserAdmin = lsAdmin.Select(s => new SelectListItem
            {
                Text = s.UserName,
                Value = s.UserId.ToString()
            });
            var lsExists = trans.getListGHForAdmin();
            ViewBag.lsExists = lsExists;
            return View();
        }
        [HttpPost]
        public ActionResult Prioritize(int userId = 0)
        {
            var kq = trans.createGHManual(userId, 0);
            if (kq.Equals("***"))
                return RedirectToAction("Prioritize", "GetHelp", new { message = "Success !!!" });
            else
                return RedirectToAction("Prioritize", "GetHelp", new { message = kq });
        }
        public ActionResult DeleteGH(int ghId = 0)
        {
            if (Session["User"] == null)
                return RedirectToAction("Login", "Account");
            var kq = trans.deleteGHOfUser(ghId);
            if (kq.Equals("***"))
                return RedirectToAction("ListGH", "GetHelp", new { message = "Success !!!" });
            else
                return RedirectToAction("ListGH", "GetHelp", new { message = kq });
        }
        public ActionResult showModalGhInfo(int userId = 0)
        {
            var model = trans.findModalInfo(userId);
            return PartialView("_ShowInfoGH", model);
        }
        //Danh sách withdraw
        public ActionResult ListWd(int page = 1, string status = "", string Fromdate = "", string Todate = "", string Username = "", string message = "")
        {
            if (Session["User"] == null)
                return RedirectToAction("Login", "Account");
            ViewBag.Msg = message;
            DateTime fromdate;
            DateTime todate;
            int Stt = -1;
            if (Fromdate == "")
                fromdate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            else
            {
                fromdate = Convert.ToDateTime(Fromdate);
                ViewBag.DateFrom = Fromdate;
            }
            if (Todate == "")
                todate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);
            else
            {
                todate = Convert.ToDateTime(Todate);
                ViewBag.Todate = Todate;
            }
            if (status != "")
                Stt = Convert.ToInt32(status);
            var ls = trans.getListWdForAdmin(Username, Stt, fromdate, todate, (page - 1) * 25, 25);
            int totalItems = ls.Item1;
            var lsGH = ls.Item2.ToList();
            ViewBag.GHs = lsGH;
            int[] myArray = Enumerable.Repeat(-1, ls.Item1).ToArray();
            return View(myArray.ToPagedList(page, 25));
        }
        // hàm tạo lệnh chuyển cho Withdraw
        public ActionResult CreateTransferOrderWd(int userGH = 0, int ghId = 0)
        {
            if (userGH == 0 || ghId == 0)
                return RedirectToAction("ListWd", "GetHelp", new { message = "Không thành công !!!" });
            var kq = trans.pairingPHAndGH(userGH, ghId);
            if (kq.Equals("***"))
            {
                return RedirectToAction("ListWd", "GetHelp", new { message = "Thành công !!!" });
            }
            else
                return RedirectToAction("ListWd", "GetHelp", new { message = kq });
        }
        public ActionResult DeleteWd(int phId = 0)
        {
            var kq = trans.deleteGHOfUser(phId);
            if (kq.Equals("***"))
                return RedirectToAction("ListWd", "GetHelp", new { message = "Success !!!" });
            else
                return RedirectToAction("ListWd", "GetHelp", new { message = kq });
        }
    }
}
